#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    img = new QImage();
    data_ = boost::make_shared<Data>();
    socket_ = boost::make_shared<Socket>(this);
    decode_ = boost::make_shared<Decode>();

    isRuning = false;
    InitDisplay();
    InitSignal();
    ui->infoTextBrowser->append("init complete!");
}

void MainWindow::InitDisplay()
{
    //    this->setWindowTitle(tr("Monitor Car"));
    //    this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏

    //    if(!(img->load("/home/liu/桌面/monitor/client/welcome.png") ) ) //加载图像
    //    {
    //        QMessageBox::information(this,
    //                                 tr("failed!"),
    //                                 tr("failed!"));
    ////        ::exit(1);
    //    }
    //    ui->picture->setPixmap(QPixmap::fromImage(*img));
    ui->addressEdit->setText("192.168.31.4");
    ui->portEdit->setText("4000");
}

void MainWindow::InitSignal()
{
//    connect(this, SIGNAL(recvReady()), this, SLOT(displayPicture()));
    connect(ui->connect, SIGNAL(clicked()), this, SLOT(connectServer()));
    connect(ui->unconnect, SIGNAL(clicked()), this, SLOT(unconnect()));
    connect(ui->quitBtn, SIGNAL(clicked()), this, SLOT(close()));

    connect(ui->goButton, SIGNAL(pressed()), this, SLOT(controlSet()));
    connect(ui->RightButton, SIGNAL(pressed()), this, SLOT(controlSet()));
    connect(ui->LeftButton, SIGNAL(pressed()), this, SLOT(controlSet()));
    connect(ui->BackButton, SIGNAL(pressed()), this, SLOT(controlSet()));

    connect(ui->goButton, SIGNAL(released()), this, SLOT(controlClean()));
    connect(ui->RightButton, SIGNAL(released()), this, SLOT(controlClean()));
    connect(ui->LeftButton, SIGNAL(released()), this, SLOT(controlClean()));
    connect(ui->BackButton, SIGNAL(released()), this, SLOT(controlClean()));
    connect(ui->shotsnapeButton, SIGNAL(released()), this, SLOT(controlClean()));

    connect(socket_.get(), SIGNAL(dataReady()), this, SLOT(process()));
}

void MainWindow::process()
{
    socket_->RecvData(data_);
    decode_->decodeData(data_);
    if (data_->getValidDataSize() != 0) {
        displayPicture();
    }
}

void MainWindow::connectServer()
{
    ui->statusLabel->setText("connect to server  ...");
    socket_->ConnectServer(ui->addressEdit->text(),
                           ui->portEdit->text().toInt());

    ui->infoTextBrowser->append("connect successful!");
//    isRuning = true;
//    if (0 == startTimer(28)){
//        ui->infoTextBrowser->append("timer start failed!");
//    }
    ui->statusLabel->setText("monitor runing ...");
}

void MainWindow::unconnect()
{
//    isRuning = false;
    //    if(!(img->load(tr("/home/liu/桌面/monitor/client/welcome.png")) ) ) //加载图像
    //    {
    //        QMessageBox::information(this,
    //                                 tr("failed!"),
    //                                 tr("failed!"));
    //        return;
    //    }
    //    ui->picture->setPixmap(QPixmap::fromImage(*img));
    //    ui->picture->update();
    socket_->DisConnectServer();
    ui->infoTextBrowser->append("connect close!");
}

int MainWindow::displayPicture()
{
    if(!(img->loadFromData((unsigned char*)data_->GetPPMptr(),
                           data_->GetPPMsize()))) //加载图像
    {
        //       QMessageBox::information(this,
        //                                tr("display failed!"),
        //                                tr("display failed!"));
        return 1;
    }
    ui->picture->setPixmap(QPixmap::fromImage(*img));
    ui->picture->update();

    return 0;
}

void MainWindow::controlSet()
{
    QPushButton *tmp = dynamic_cast<QPushButton *>(sender());
    DataUtil::Direct tmp_d;

    if("go" == tmp->whatsThis()){
        tmp_d = DataUtil::go;
        data_->setDirect(tmp_d);
    }else if("left" == tmp->whatsThis()){
        tmp_d = DataUtil::left;
        data_->setDirect(tmp_d);
    }else if("right" == tmp->whatsThis()){
        tmp_d = DataUtil::right;
        data_->setDirect(tmp_d);
    }else if("back" == tmp->whatsThis()){
        tmp_d = DataUtil::back;
        data_->setDirect(tmp_d);
    }

    socket_->SendData(data_);
}

void MainWindow::controlClean()
{
    QPushButton *tmp = dynamic_cast<QPushButton *>(sender());
    DataUtil::Direct tmp_d = DataUtil::stop;

    if("go" == tmp->whatsThis()) {
        data_->setDirect(tmp_d);
    } else if("left" == tmp->whatsThis()) {
        data_->setDirect(tmp_d);
    } else if("right" == tmp->whatsThis()) {
        data_->setDirect(tmp_d);
    } else if("back" == tmp->whatsThis()) {
        data_->setDirect(tmp_d);
    } else if("savePicture" == tmp->whatsThis()) {
        QFile tmpFile("test.ppm");
        if (!tmpFile.open(QIODevice::WriteOnly)) {
            std::cout << "open file failed!" << std::endl;
            return;
        }
        int writebyte = data_->GetPPMsize(), num;
        while (writebyte > 0) {
            num = tmpFile.write((char*)data_->GetPPMptr(), writebyte);
            writebyte -= num;
        }
        tmpFile.close();
    }
    socket_->SendData(data_);
}

//void MainWindow::mousePressEvent(QMouseEvent *e)
//{
//    this->windowPos = this->pos();
//    this->mousePos = e->globalPos();
//    this->dPos = mousePos - windowPos;
//    e->ignore();
//}

//void MainWindow::mouseMoveEvent(QMouseEvent *e)
//{
//    this->move(e->globalPos() - this->dPos);
//    e->ignore();
//}

MainWindow::~MainWindow()
{
    delete ui;
    delete img;
}


